<?php
/**
 * Created by PhpStorm.
 * User: Fonny
 * Date: 14-8-27
 * Time: 上午8:58
 */

//连接数据库服务器
/*
参数 描述
servername 可选。规定要连接的服务器。默认是 "localhost:3306"。
username 可选。规定登录所使用的用户名。默认值是拥有服务器进程的用户的名称。
password 可选。规定登录所用的密码。默认是 ""。 */
//$con=mysql_connect('localhost','root','root');
$con = mysqli_connect('localhost', 'root', 'root');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

//创建数据库
$sql = 'CREATE DATABASE php_study;';

if (mysqli_query($con, $sql)) {
    echo "Database created";
} else if (mysqli_errno($con) != 1007) {
    echo "Error creating database:\n" . mysqli_errno($con) . "\n" . mysqli_error($con);
}

//创建表
mysqli_select_db($con, "php_study");

$sql = "CREATE TABLE tbl_user2
(
col_user_id int NOT NULL AUTO_INCREMENT,
PRIMARY key (col_user_id),
col_user_name varchar(24),
col_user_passwd blob
);";

if (mysqli_query($con, $sql)) {
    printf("Table created!");
} else if (mysqli_errno($con) == 1050) {
//    printf(mysqli_error($con));
} else {
    printf("Table created ErrorCode: %d\n%s\n", mysqli_errno($con), mysqli_error($con));
}

//插入数据
$sql = "insert into tbl_user2 (col_user_name, col_user_passwd)
value ('testUser1','abc123');";
if (mysqli_query($con, $sql)) {
    printf("Insert user complete");
} else {
    printf("Insert user Error: %d\n", mysqli_errno($con));
}

//获取数据
$sql = "select col_user_name, col_user_passwd from tbl_user2 limit 100;";
if (mysqli_query($con,$sql))
{
    mysqli_fetch_row($con)
}
mysqli_close($con);
?>